/**
 * @file ByteConver.hpp
 * @brief 提供字符串转int大小端函数接口 
 * @details 支持uint16_t uint32_t uint64_t 的大小端转换接口
 * @author yupeijin
 * @date 2022年10月14日
 * @license MIT Apache2.0
 * Copyright (C), 2010-2022 Jouav Co., Ltd.
 */

#pragma once

#ifdef POSIX
#include <arpa/inet.h>
#endif

#ifdef WIN32
#include <stdlib.h>
#endif

#include <stddef.h>
#include <stdint.h>



// Reading and writing of little and big-endian numbers from memory
// TODO: Optimized versions, with direct read/writes of
// integers in host-endian format, when the platform supports it.
/**
* @brief 将uint8_t放到buf制定的位置
* @param memory buf容器，长度要大于偏移量
* @param offset 偏移量，memory的第offset的位置存放
* @param v 要存放的uint8_t内容
* @return
*/  
inline void Set8(void* memory, size_t offset, uint8_t v) 
{
	if (memory) 
	{
	static_cast<uint8_t*>(memory)[offset] = v;
	}
}

/**
* @brief 在buf的制定位置取值
* @param memory buf容器，长度要大于偏移量
* @param offset 偏移量，memory的第offset的位置存放
* @return -1：失败，其他在offset取到的uint8_t的值
*/  
inline uint8_t Get8(const void* memory, size_t offset) 
{
	if (memory) 
	{
	return static_cast<const uint8_t*>(memory)[offset];
	}
	return -1;
}

/**
* @brief 将uint16_t的值按照大端模式放入buf中
* @param memory buf容器
* @param v 需要放入buf中的uint16_t类型的值
* @return 
*/  
inline void SetBE16(void* memory, uint16_t v) 
{
	Set8(memory, 0, static_cast<uint8_t>(v >> 8));
	Set8(memory, 1, static_cast<uint8_t>(v >> 0));
	return;
}

/**
* @brief 将uint32_t的值按照大端模式放入buf中
* @param memory buf容器
* @param v 需要放入buf中的uint32_t类型的值
* @return 
*/  
inline void SetBE32(void* memory, uint32_t v) 
{
	Set8(memory, 0, static_cast<uint8_t>(v >> 24));
	Set8(memory, 1, static_cast<uint8_t>(v >> 16));
	Set8(memory, 2, static_cast<uint8_t>(v >> 8));
	Set8(memory, 3, static_cast<uint8_t>(v >> 0));
	return;
}

/**
* @brief 将uint64_t的值按照大端模式放入buf中
* @param memory buf容器
* @param v 需要放入buf中的uint64_t类型的值
* @return 
*/  
inline void SetBE64(void* memory, uint64_t v) 
{
	Set8(memory, 0, static_cast<uint8_t>(v >> 56));
	Set8(memory, 1, static_cast<uint8_t>(v >> 48));
	Set8(memory, 2, static_cast<uint8_t>(v >> 40));
	Set8(memory, 3, static_cast<uint8_t>(v >> 32));
	Set8(memory, 4, static_cast<uint8_t>(v >> 24));
	Set8(memory, 5, static_cast<uint8_t>(v >> 16));
	Set8(memory, 6, static_cast<uint8_t>(v >> 8));
	Set8(memory, 7, static_cast<uint8_t>(v >> 0));
	return;
}

/**
* @brief 在buf中按大端模式获取uint16_t类型的值
* @param memory buf容器
* @return 获取到的uint16_t类型的值
*/  
inline uint16_t GetBE16(const void* memory) {
  return static_cast<uint16_t>((Get8(memory, 0) << 8) |
                             (Get8(memory, 1) << 0));
}

/**
* @brief 在buf中按大端模式获取uint32_t类型的值
* @param memory buf容器
* @return 获取到的uint32_t类型的值
*/  
inline uint32_t GetBE32(const void* memory) 
{
	return (static_cast<uint32_t>(Get8(memory, 0)) << 24) |
      (static_cast<uint32_t>(Get8(memory, 1)) << 16) |
      (static_cast<uint32_t>(Get8(memory, 2)) << 8) |
      (static_cast<uint32_t>(Get8(memory, 3)) << 0);
}


/**
* @brief 在buf中按大端模式获取uint64_t类型的值
* @param memory buf容器
* @return 获取到的uint64_t类型的值
*/  
inline uint64_t GetBE64(const void* memory) {
  return (static_cast<uint64_t>(Get8(memory, 0)) << 56) |
      (static_cast<uint64_t>(Get8(memory, 1)) << 48) |
      (static_cast<uint64_t>(Get8(memory, 2)) << 40) |
      (static_cast<uint64_t>(Get8(memory, 3)) << 32) |
      (static_cast<uint64_t>(Get8(memory, 4)) << 24) |
      (static_cast<uint64_t>(Get8(memory, 5)) << 16) |
      (static_cast<uint64_t>(Get8(memory, 6)) << 8) |
      (static_cast<uint64_t>(Get8(memory, 7)) << 0);
}

/**
* @brief 将uint16_t的值按照小端模式放入buf中
* @param memory buf容器
* @param v 需要放入buf中的uint16_t类型的值
* @return 
*/  
inline void SetLE16(void* memory, uint16_t v) {
	Set8(memory, 0, static_cast<uint8_t>(v >> 0));
	Set8(memory, 1, static_cast<uint8_t>(v >> 8));
	return;
}

/**
* @brief 将uint32_t的值按照小端模式放入buf中
* @param memory buf容器
* @param v 需要放入buf中的uint32_t类型的值
* @return 
*/  
inline void SetLE32(void* memory, uint32_t v) 
{
	Set8(memory, 0, static_cast<uint8_t>(v >> 0));
	Set8(memory, 1, static_cast<uint8_t>(v >> 8));
	Set8(memory, 2, static_cast<uint8_t>(v >> 16));
	Set8(memory, 3, static_cast<uint8_t>(v >> 24));
	return;
}

/**
* @brief 将uint64_t的值按照小端模式放入buf中
* @param memory buf容器
* @param v 需要放入buf中的uint64_t类型的值
* @return 
*/ 
inline void SetLE64(void* memory, uint64_t v) 
{
	Set8(memory, 0, static_cast<uint8_t>(v >> 0));
	Set8(memory, 1, static_cast<uint8_t>(v >> 8));
	Set8(memory, 2, static_cast<uint8_t>(v >> 16));
	Set8(memory, 3, static_cast<uint8_t>(v >> 24));
	Set8(memory, 4, static_cast<uint8_t>(v >> 32));
	Set8(memory, 5, static_cast<uint8_t>(v >> 40));
	Set8(memory, 6, static_cast<uint8_t>(v >> 48));
	Set8(memory, 7, static_cast<uint8_t>(v >> 56));
	return;
}

/**
* @brief 在buf中按小端模式获取uint16_t类型的值
* @param memory buf容器
* @return 获取到的uint16_t类型的值
*/  
inline uint16_t GetLE16(const void* memory) 
{
	return static_cast<uint16_t>((Get8(memory, 0) << 0) |
                             (Get8(memory, 1) << 8));
}

/**
* @brief 在buf中按小端模式获取uint32_t类型的值
* @param memory buf容器
* @return 获取到的uint32_t类型的值
*/  
inline uint32_t GetLE32(const void* memory) {
	return (static_cast<uint32_t>(Get8(memory, 0)) << 0) |
		(static_cast<uint32_t>(Get8(memory, 1)) << 8) |
		(static_cast<uint32_t>(Get8(memory, 2)) << 16) |
		(static_cast<uint32_t>(Get8(memory, 3)) << 24);
}

/**
* @brief 在buf中按小端模式获取uint64_t类型的值
* @param memory buf容器
* @return 获取到的uint64_t类型的值
*/ 
inline uint64_t GetLE64(const void* memory) {
  return (static_cast<uint64_t>(Get8(memory, 0)) << 0) |
      (static_cast<uint64_t>(Get8(memory, 1)) << 8) |
      (static_cast<uint64_t>(Get8(memory, 2)) << 16) |
      (static_cast<uint64_t>(Get8(memory, 3)) << 24) |
      (static_cast<uint64_t>(Get8(memory, 4)) << 32) |
      (static_cast<uint64_t>(Get8(memory, 5)) << 40) |
      (static_cast<uint64_t>(Get8(memory, 6)) << 48) |
      (static_cast<uint64_t>(Get8(memory, 7)) << 56);
}

// Check if the current host is big endian.
/**
* @brief 判断当前系统是否是大端模式
* @return true 大端；false 小端
*/ 
inline bool IsHostBigEndian() 
{
	static const int number = 1;
	return 0 == *reinterpret_cast<const char*>(&number);
}

/**
* @brief 将uint16_t转成网络字节序
* @return 转换后的uint16_t的值
*/ 
inline uint16_t HostToNetwork16(uint16_t n) 
{
	uint16_t result;
	SetBE16(&result, n);
	return result;
}

/**
* @brief 将uint32_t转成网络字节序
* @return 转换后的uint32_t的值
*/ 
inline uint32_t HostToNetwork32(uint32_t n) 
{
	uint32_t result;
	SetBE32(&result, n);
	return result;
}

/**
* @brief 将uint64_t转成网络字节序
* @return 转换后的uint64_t的值
*/ 
inline uint64_t HostToNetwork64(uint64_t n) 
{
	uint64_t result;
	SetBE64(&result, n);
	return result;
}
